const Storage = {};
/** 获取Storage对象 */
const storage = (type = 'local') => {
  return type == "local" ? window.localStorage : window.sessionStorage;
}
/**
 * 获取对应缓存值JSON对象
 * @param {string} key 键
 * @param {string} type 默认为localStorage, 'session'为sessionStorage
 * @return {object} 对应的值JSON对象
 */
Storage.get = (key, type = 'local') => {
  var js = storage(type).getItem(key);
  return /^[\\{|\\[].+[\\}|\\]]$/.test(js) ? JSON.parse(js) : js;
};
/**
 * 设置存储缓存取对象JSON
 * @param {string} key 键
 * @param {string} value 值(JSON对象)
 * @param {string} type 默认为localStorage, 'session'为sessionStorage
 */
Storage.set = (key, value, type = 'local') => {
  storage(type).setItem(key, JSON.stringify(value));
};
/**
 * 删除对应的缓存对象JSON
 * @param {string} key 键
 * @param {string} type 默认为localStorage, 'session'为sessionStorage
 */
Storage.remove = (key, type = 'local') => {
  storage(type).removeItem(key);
};
/**
 * 清空所有的缓存对象JSON
 * @param {string} type 默认为localStorage, 'session'为sessionStorage
 */
Storage.clear = (type = 'local') => {
  storage(type).clear();
};

export default Storage;
